package com.reider.log4AS3.manager
{
	import com.reider.log4AS3.Logger;
	import com.reider.log4AS3.core.ConfigXMLLoader;
	import com.reider.log4AS3.core.LogFilter;
	import com.reider.log4AS3.core.LoggerContext;
	import com.reider.log4AS3.core.XMLConfigFactory;
	import com.reider.log4AS3.core.log_internal;
	import com.reider.log4AS3.dataholder.Log;
	import com.reider.log4AS3.event.ConfigXMLLoaderEvent;
	import com.reider.log4AS3.interfaces.ILogger;
	import com.reider.log4AS3.interfaces.ILoggerContext;
	import com.reider.log4AS3.layouts.PatternLayout;
	import com.reider.log4AS3.utils.BufferedCaller;
	import com.reider.log4AS3.utils.LogUtils;
	import com.reider.log4AS3.utils.ObjectContainer;
	
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;

	/**
	 * @private
	 */
	 
	public class LogManager
	{
		private static var instance:LogManager
		
		private var configLoader:ConfigXMLLoader 
		private var loggerScopes:Array
		
		private var _defaultContext:ILoggerContext
		private var _logConfigURL:String = "logconfig.xml";
		private var _logEnabled:Boolean = true
		
		//--------------------------------------------------------------------------
	    //
	    //  Geters, Seters
	    //
	    //--------------------------------------------------------------------------

		public function get defaultContext():ILoggerContext
		{
			if(!_defaultContext){
				_defaultContext = new LoggerContext()
			}
			
			return _defaultContext;
		}

		public function set defaultContext(value:ILoggerContext):void
		{
			_defaultContext = value;
		}

		public function set logEnabled(value:Boolean):void
		{
			_logEnabled = value
		}
		
		public function get logEnabled():Boolean
		{
			return _logEnabled
		}
		
		public function get logConfigURL():String
		{
			return _logConfigURL
		}
		
		public function set logConfigURL(value:String):void
		{
			_logConfigURL = value
		}

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function LogManager(enforcer:Key)
		{
			if(enforcer == null){
				throw new ArgumentError("The enforcer can't be null")
			}
			
			loggerScopes = new Array()
			configLoader = new ConfigXMLLoader()
			configLoader.addEventListener(ConfigXMLLoaderEvent.CONFIG_LOADED, onConfigLoaded)
			configLoader.addEventListener(ConfigXMLLoaderEvent.CONFIG_ERROR, onConfigLoadError)
			
			setTimeout(init, 10)
		}
		
		//--------------------------------------------------------------------------
	    //
	    //  Public methods
	    //
	    //--------------------------------------------------------------------------
		
		public function getLogger(logTarget:Object):Logger
		{
			return defaultContext.getLogger( logTarget )
		}
		
		public function addLog(log:Log):void
		{
			XMLConfigFactory
			PatternLayout
			/*if(!_logEnabled) return
			
			var relTime:int
			
			if(relativeTimes.hasKey(log.loggerName)){
				var startTime:int = int(relativeTimes.getObj(log.loggerName))
				relTime = getTimer()-startTime
			}else{
				relativeTimes.add(log.loggerName, getTimer())
				relTime = 0;
			}
			
			log.log_internal::time = new Date()
			log.log_internal::reltime = relTime
			
			logFilterBuffer.addLog(log);*/
		}
		
		//--------------------------------------------------------------------------
		//
		//  Private methods
		//
		//--------------------------------------------------------------------------
		
		private function init():void
		{
			if(_logEnabled){
				loadConfig()
			}
		}
		
		private function loadConfig():void
		{
			configLoader.loadURL( logConfigURL )
		}
		
		private function setLoggerScopes(scopes:Array):void
		{
			loggerScopes = scopes
			defaultContext.setLoggerScopes( scopes )
		}
		
		//--------------------------------------------------------------------------
		//
		//  Callback methods
		//
		//--------------------------------------------------------------------------
		
		private function onConfigLoaded(event:ConfigXMLLoaderEvent):void
		{
			setLoggerScopes( event.loggerScopes )
		}
		
		private function onConfigLoadError(event:ConfigXMLLoaderEvent):void
		{
			trace("::: LOG4AS3 XML LOAD ERROR ::: "+event.errorMessage)
		}
		
		//--------------------------------------------------------------------------
	    //
	    //  Static methods
	    //
	    //--------------------------------------------------------------------------
		
		public static function getInstance():LogManager
		{
			if(!instance){
				instance =  new LogManager(new Key())
			}
			
			return instance
		}
	}
}
internal class Key {}